package com.all.play.myapplication.activity;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.FrameLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.all.play.myapplication.R;
import com.all.play.myapplication.base.BaseFragment;
import com.all.play.myapplication.fragment.HomeFragment;
import com.all.play.myapplication.fragment.MoreFragment;
import com.all.play.myapplication.fragment.NewsFragment;
import com.all.play.myapplication.fragment.ProductFragment;
import com.all.play.myapplication.fragment.ProviderFragment;
import com.jaeger.library.StatusBarUtil;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.jzvd.JZVideoPlayer;

public class MainActivity extends AppCompatActivity {

    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    @BindView(R.id.rb_home)
    RadioButton rbHome;
    @BindView(R.id.rb_type)
    RadioButton rbType;
    @BindView(R.id.rb_community)
    RadioButton rbCommunity;
    @BindView(R.id.rb_cart)
    RadioButton rbCart;
    @BindView(R.id.rb_user)
    RadioButton rbUser;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;

    private ArrayList<BaseFragment> fragments;

    //记录选中的位置
    private int position = 0;
    //记录临时变量
    private Fragment tempFragemnt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        StatusBarUtil.setTranslucentForImageViewInFragment(MainActivity.this, null);
        //StatusBarUtil.setTransparent(MainActivity.this);
        //StatusBarUtil.setColorForSwipeBack(MainActivity.this,0);
        //StatusBarUtil.setColor(MainActivity.this, ContextCompat.getColor(MainActivity.this,R.color.translate), 30);
        //初始化fragment
        initFragment();

        //对RadioGroup进行监听
        initRGListener();
    }

    /**
     * 监听切换滑动
     */
    private void initRGListener() {

        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        position = 0;
                        //StatusBarUtil.setColor(MainActivity.this, ContextCompat.getColor(MainActivity.this,R.color.colorAccent));
                        //StatusBarUtil.setColor(MainActivity.this,0);
                        //StatusBarUtil.setTransparent(MainActivity.this);
                        break;
                    case R.id.rb_type:
                        position = 1;
                        break;
                    case R.id.rb_community:
                        position = 2;
                        break;
                    case R.id.rb_cart:
                        position = 3;
                        break;
                    case R.id.rb_user:
                        position = 4;
                        break;

                    default:

                        break;
                }

                //根据位置选取fragment
                BaseFragment baseFragment = getFragment(position);

                /*
                * params1,上次显示的fragment,缓存中的
                * params2,当前正要显示的
                * */
                switchFragment(tempFragemnt, baseFragment);

            }
        });


        rgMain.check(R.id.rb_home);

    }


    /**
     * 根据位置,选取fragment
     *
     * @param position
     * @return
     */
    private BaseFragment getFragment(int position) {
        if (fragments != null && fragments.size() > 0) {
            for (int i = 0; i < fragments.size(); i++) {
                BaseFragment baseFragment = fragments.get(position);
                return baseFragment;
            }
        }

        return null;
    }

    /**
     * 根据判断,切换fragment
     *
     * @return
     */
    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {
        if (tempFragemnt != nextFragment) {
            tempFragemnt = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                //判断 nextFragment 是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.add(R.id.frameLayout, nextFragment).commit();
                } else {
                    //隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
            JZVideoPlayer.releaseAllVideos();
        }

    }

    /**
     * 初始化fragmnet
     */
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new ProductFragment());
        fragments.add(new NewsFragment());
        fragments.add(new ProviderFragment());
        fragments.add(new MoreFragment());
    }


    @Override
    protected void onPause() {
        super.onPause();
        JZVideoPlayer.releaseAllVideos();
    }

    @Override
    public void onBackPressed() {
        if (JZVideoPlayer.backPress()) {
            return;
        }
        super.onBackPressed();
    }



}
